import axios from 'axios';

// 创建一个简单的axios实例
// 现在指向Gateway，统一入口并实现负载均衡
const apiClient = axios.create({
  baseURL: 'http://localhost:9527',
  timeout: 10000
});

// 简单的响应拦截器
apiClient.interceptors.response.use(
  response => response,
  error => Promise.reject(error)
);

// 基础的GET请求
export const get = async (url, params = {}) => {
  try {
    const response = await apiClient.get(url, { params });
    return response.data;
  } catch (error) {
    console.error('请求失败:', error);
    throw error;
  }
};

// 基础的POST请求
export const post = async (url, data) => {
  try {
    console.log('发送POST请求:', { url, data });
    const response = await apiClient.post(url, data);
    console.log('POST请求响应:', { url, status: response.status, data: response.data });
    return response.data;
  } catch (error) {
    console.error('POST请求失败:', { url, error: error.message, response: error.response });
    throw error;
  }
};

// 基础的PUT请求
export const put = async (url, data) => {
  try {
    console.log('发送PUT请求:', { url, data });
    const response = await apiClient.put(url, data);
    console.log('PUT请求响应:', { url, status: response.status, data: response.data });
    return response.data;
  } catch (error) {
    console.error('PUT请求失败:', { url, error: error.message, response: error.response });
    throw error;
  }
};

// 基础的DELETE请求
export const del = async (url) => {
  try {
    console.log('发送DELETE请求:', { url });
    const response = await apiClient.delete(url);
    console.log('DELETE请求响应:', { url, status: response.status, data: response.data });
    return response.data;
  } catch (error) {
    console.error('DELETE请求失败:', { url, error: error.message, response: error.response });
    throw error;
  }
};

export default {
  get,
  post,
  put,
  del
};